package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.pojo.Information;
import cn.edu.cimv.service.ContentService;
import cn.edu.cimv.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * Created by 门铎 on 2016/6/23.
 */
@Controller

public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private ContentService contentService;
    private static Logger logger =LoggerFactory.getLogger(LoginController.class);
    @RequestMapping("/cimv/manage")
    public String loginPage() {
        return "manage/login";
    }

    @RequestMapping("cimv/login")
    public String login(Model model, HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
        //用加密

        Information information = loginService.checkIn(username, password);

        if (information.getMessage().equals(InfoConstant.LOGIN_RIGHT)) {
            logger.debug( information.getDate() + "   "+username +"登陆成功");
            session.setAttribute("username", username);
            String roleId = loginService.getRoleId(username);
            String userId = loginService.getUserId(username);
            session.setAttribute("roleId",roleId);
            session.setAttribute("userId",userId);
            model.addAttribute("system",loginService.getSystemPraa());
            return "manage/index";
        } else {
            logger.error( information.getDate() + "   "+username +"试图登录！登录失败");
            model.addAttribute("information", information);
            return "manage/login";
        }
    }

    /*@RequestMapping("cimv/manage/redirection/{pageNum}")
    public String redirect(@PathVariable("pageNum")String pageNum,Model model){
        model.addAttribute("pageNum",pageNum);
        switch(pageNum){

            case "0000":{return "manage/index";}
            case "0002":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
            case "0003":{return "forward:/cimv/user/list";}
            case "0004":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
           *//* case "0005":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }*//*
            case "0006":{
                return null;
            }
            case "0007":{
                return null;
            }
            case "0008":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
            case "0009":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
            case "0010":{
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "forward:/cimv/article/list";
            }


            case "0011":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
            case "0012":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/editorSinglePage";
            }
            default:{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
        }
    }*/
  /*  @RequestMapping("cimv/manage/page/{pageNum}")
    public String page(@PathVariable("pageNum")String pageNum,Model model){
        model.addAttribute("pageNum",pageNum);
        switch(pageNum){

            case "0000":{return "manage/index";}
            case "0002":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0003":{return null;}
            case "0004":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0005":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0006":{
                return null;
            }
            case "0007":{
                return null;
            }
            case "0008":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0009":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0010":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0011":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            case "0012":{
                //将单页面内容加载到页面
                model.addAttribute("content",contentService.getContentByPageNum(pageNum));
                //对应的版块加入到页面
                model.addAttribute("page",contentService.getPageByPageNum(pageNum));
                return "manage/show";
            }
            default:{
                return null;
            }
        }
    }*/
}
